<extend name="Public/base"/>
<block name="style">
    <link rel="stylesheet" href="__JS__/codemirror/codemirror.css">
    <link rel="stylesheet" href="__JS__/codemirror/theme/{:C('codemirror_theme')}.css">
    <style>
        .CodeMirror, #preview_window {
            width: 700px;
            height: 500px;
        }

        #preview_window.loading {
            background: url('__JS__/thinkbox/skin/default/tips_loading.gif') no-repeat center;
        }

        #preview_window textarea {
            display: none;
        }
    </style>
</block>
<block name="body">
    <!-- ������->
    <div class="main-title">
        <h2>{:L("_FAST_OPERATION_")}</h2>
    </div>
    <!-- /������-->

    <div class="with-padding">
        <a id="addpack" class="btn btn-default" href="{:U('addpack')}">{:L("_NEW_PATCH_")}</a><!--
        <a id="use" class="btn" href="" autocomplete="off">{:L("_USE_PATCHES_")}</a>-->
    </div>

    <!-- Ӧ���б�-->
    <div class="with-padding">
        <table class="table table-bordered table-striped">
            <thead>
            <tr>
                <th width="200">{:L("_NAME_")}</th>
                <th>{:L("_INTRODUCTION_")}</th>
                <th width="200">{:L("_AUTHOR_")}</th>
                <th width="80">{:L("_DATA_SIZE_")}</th>
                <th width="300">{:L("_PATCH_CREATION_TIME_")}</th>

                <th width="300">{:L("_LAST_MODIFICATION_TIME_")}</th>
                <th width="150">{:L("_OPERATION_")}</th>
            </tr>
            </thead>
            <tbody>
            <volist name="list" id="data">
                <tr>
                    <td><a href="{:U('addpack?id='.$data['id'])}">{$data.title}</a></td>
                    <td>{$data.des}</td>
                    <td>{$data.author}</td>
                    <td>{$data.size}</td>
                    <td>{$data.ctime}</td>
                    <td>{$data.mtime}</td>

                    <td class="action">

                        <a class="ajax-get confirm use db-import" style="cursor: pointer;color: red;font-weight: bold" href="{:U('use_pack?id='.$data['id'])}">{:L("_USE_")}</a>&nbsp;
                        <a class=" use " href="{:U('addpack?id='.$data['id'])}">{:L("_EDIT_")}</a>&nbsp;
                        <a id="" class="db-import view" style="cursor: pointer" data="{$data.id}"
                           type="button">{:L("_SEE_")}</a>&nbsp;
                        <a class="ajax-get confirm" href="{:U('del_pack?id='.$data['id'])}"
                                >{:L("_DELETE_")}</a>
                    </td>
                </tr>
            </volist>
            </tbody>
        </table>
    </div>
    <!-- /Ӧ���б�-->
</block>

<block name="script">
    <script type="text/javascript" src="__JS__/codemirror/codemirror.js"></script>
    <script type="text/javascript" src="__JS__/codemirror/clike.js"></script>
    <script type="text/javascript" src="__JS__/codemirror/sql.js"></script>
    <script type="text/javascript" src="__JS__/thinkbox/jquery.thinkbox.js"></script>
    <script type="text/javascript">


        $(function () {

            $('.view').click(function () {
                var preview_url = '{:U("view")}';
                var title = $(this).attr('data');
                $.post(preview_url, {title: title}, function (data) {
                    $.thinkbox('<div id="preview_window" class="loading"><textarea></textarea></div>', {
                        afterShow: function () {
                            var codemirror_option = {
                                lineNumbers: true,
                                matchBrackets: true,
                                mode: "sql",
                                indentUnit: 4,
                                gutter: true,
                                fixedGutter: true,
                                indentWithTabs: true,
                                readOnly: true,
                                lineWrapping: true,
                                height: 500,
                                enterMode: "keep",
                                tabMode: "shift",
                                theme: "{:C('CODEMIRROR_THEME')}"
                            };
                            var preview_window = $("#preview_window").removeClass(".loading").find("textarea");
                            var editor = CodeMirror.fromTextArea(preview_window[0], codemirror_option);
                            editor.setValue(data);
                            $(window).resize();
                        },

                        title: {:L('_VIEW_CONTENT_WITH_SINGLE_')},
                        unload: true,
                        actions: ['close'],
                        drag: true
                    });
                });
                return false;
            });


            var $form = $("#export-form"), $export = $("#export"), tables
            $optimize = $("#optimize"), $repair = $("#repair");

            $optimize.add($repair).click(function () {
                $.post(this.href, $form.serialize(), function (data) {
                    if (data.status) {
                        updateAlert(data.info, 'alert-success');
                    } else {
                        updateAlert(data.info, 'alert-error');
                    }
                    setTimeout(function () {
                        $('#top-alert').find('button').click();
                        $(that).removeClass('disabled').prop('disabled', false);
                    }, 1500);
                }, "json");
                return false;
            });

            $export.click(function () {
                $export.parent().children().addClass("disabled");
                $export.html("{:L("_IS_SENDING_A_BACKUP_REQUEST_")}..");
                $.post(
                        $form.attr("action"),
                        $form.serialize(),
                        function (data) {
                            if (data.status) {
                                tables = data.tables;
                                $export.html(data.info + {:L("_START_BACKUP_PLEASE_DONT_CLOSE_THIS_PAGE_WITH_DOUBLE_")});
                                backup(data.tab);
                                window.onbeforeunload = function () {
                                    return {:L("_IS_THE_BACKUP_DATABASE_PLEASE_DONT_TURN_OFF_WITH_DOUBLE_")}
                                }
                            } else {
                                updateAlert(data.info, 'alert-error');
                                $export.parent().children().removeClass("disabled");
                                $export.html({:L("_IMMEDIATE_BACKUP_WITH_DOUBLE_")});
                                setTimeout(function () {
                                    $('#top-alert').find('button').click();
                                    $(that).removeClass('disabled').prop('disabled', false);
                                }, 1500);
                            }
                        },
                        "json"
                );
                return false;
            });

            function backup(tab, status) {
                status && showmsg(tab.id, "{:L("_START_BACK_UP_")}..(0%)");
                $.get($form.attr("action"), tab, function (data) {
                    if (data.status) {
                        showmsg(tab.id, data.info);

                        if (!$.isPlainObject(data.tab)) {
                            $export.parent().children().removeClass("disabled");
                            $export.html({:L("_BACK_UP_CLICK_ON_THE_BACKUP_WITH_DOUBLE_")});
                            window.onbeforeunload = function () {
                                return null
                            }
                            return;
                        }
                        backup(data.tab, tab.id != data.tab.id);
                    } else {
                        updateAlert(data.info, 'alert-error');
                        $export.parent().children().removeClass("disabled");
                        $export.html({:L("_IMMEDIATE_BACKUP_WITH_DOUBLE_")});
                        setTimeout(function () {
                            $('#top-alert').find('button').click();
                            $(that).removeClass('disabled').prop('disabled', false);
                        }, 1500);
                    }
                }, "json");

            }


        });


        function showmsg(id, msg) {
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    </script>
</block>